﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace be.metanous.mvvm.converters
{
    public class VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (DependencyProperty.UnsetValue.Equals(value))
            {
                var mode = parameter is VisibilityConverterParameter
                          ? (VisibilityConverterParameter)parameter
                          : VisibilityConverterParameter.UnsetToCollapsed;
                switch (mode)
                {

                    case VisibilityConverterParameter.UnsetToCollapsed:
                        return Visibility.Collapsed;

                    case VisibilityConverterParameter.UnsetToHidden:
                        return Visibility.Hidden;
                }
            }

            if (value is bool)
            {
                var mode = parameter is VisibilityConverterParameter
                           ? (VisibilityConverterParameter)parameter
                           : VisibilityConverterParameter.BooleanToCollapsed;
                var b = (bool)value;
                switch (mode)
                {
                    case VisibilityConverterParameter.BooleanToCollapsed:
                        return b ? Visibility.Visible : Visibility.Collapsed;

                    case VisibilityConverterParameter.BooleanToHidden:
                        return b ? Visibility.Visible : Visibility.Hidden;

                    case VisibilityConverterParameter.InverseBooleanToCollapsed:
                        return b ? Visibility.Collapsed : Visibility.Visible;

                    case VisibilityConverterParameter.InverseBooleanToHidden:
                        return !b ? Visibility.Visible : Visibility.Hidden;

                }
            }

            if (value == null)
            {
                var mode = parameter is VisibilityConverterParameter
                           ? (VisibilityConverterParameter)parameter
                           : VisibilityConverterParameter.NullToCollapsed;
                switch (mode)
                {

                    case VisibilityConverterParameter.NullToCollapsed:
                        return Visibility.Collapsed;

                    case VisibilityConverterParameter.NullToHidden:
                        return Visibility.Hidden;

                }
            }
            else
            {
                var mode = parameter is VisibilityConverterParameter
                   ? (VisibilityConverterParameter)parameter
                   : VisibilityConverterParameter.NullToCollapsed;
                switch (mode)
                {

                    case VisibilityConverterParameter.InverseNullToCollapsed:
                        return Visibility.Collapsed;

                    case VisibilityConverterParameter.InverseNullToHidden:
                        return Visibility.Hidden;

                }
            }

            return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
